package com.lancoo.edu.platform.operation.backupRestore.service.bo;

import cn.hutool.core.bean.BeanUtil;
import com.lancoo.edu.platform.operation.backupRestore.constant.BackupRestoreConstant;
import com.lancoo.edu.platform.operation.backupRestore.domain.bo.UpdateTaskBo;
import com.lancoo.edu.platform.operation.backupRestore.domain.bo.UpdateTaskTurnBo;
import com.lancoo.edu.platform.operation.backupRestore.domain.po.BrSysTaskInfo;
import com.lancoo.edu.platform.operation.backupRestore.domain.vo.BrSysTaskInfoVo;
import com.lancoo.edu.platform.operation.backupRestore.service.po.BrSysTaskInfoService;
import com.lancoo.edu.platform.operation.backupRestore.service.timer.ScheduleTask;
import com.lancoo.edu.platform.operation.commons.exception.ServiceException;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * 定时任务业务处理
 * @author lzh
 */
@Service
@AllArgsConstructor
public class BrScheduleTaskBoService {

    private final BrSysTaskInfoService brSysTaskInfoService;
    private final ScheduleTask scheduleTask;

    /**
     * 获取定时任务
     *
     * @param taskMethod 定时任务方法
     * @return 定时任务信息
     */
    public BrSysTaskInfoVo getScheduleTask(String taskMethod) {
        BrSysTaskInfo brSysTaskInfo = brSysTaskInfoService.getByTaskMethod(taskMethod);
        return BeanUtil.copyProperties(brSysTaskInfo, BrSysTaskInfoVo.class);
    }

    /**
     * 更新定时任务
     *
     * @param updateTaskBo 更新定时任务接收实体类
     */
    public void updateTask(UpdateTaskBo updateTaskBo) {
        // 1、判断参数
        if (Objects.equals(updateTaskBo.getTaskType(), BackupRestoreConstant.TASK_TYPE_CRON) && StringUtils.isBlank(updateTaskBo.getTaskCron())) {
            throw new ServiceException("定时任务类型为cron，必须设置cron");
        }
        if (Objects.equals(updateTaskBo.getTaskType(), BackupRestoreConstant.TASK_TYPE_INTERVAL) && Objects.isNull(updateTaskBo.getInterval())) {
            throw new ServiceException("定时任务类型为interval，必须设置interval");
        }
        // 1、查询数据库备份任务
        BrSysTaskInfo brSysTaskInfo = brSysTaskInfoService.getById(updateTaskBo.getTaskId());
        Optional.ofNullable(brSysTaskInfo).orElseThrow(() -> new ServiceException("定时任务不存在"));
        // 2、更新
        brSysTaskInfo.setFrequency(updateTaskBo.getInterval());
        brSysTaskInfo.setTaskCorn(updateTaskBo.getTaskCron());
        brSysTaskInfo.setUpdateTime(null);
        brSysTaskInfoService.updateById(brSysTaskInfo);
        // 3、更新定时任务
        scheduleTask.updateTrigger(brSysTaskInfo);
    }

    /**
     * 更新定时任务打开状态
     *
     * @param updateTaskTurnBo 更新定时任务开关接收实体类
     */
    public void updateTaskTurnBo(UpdateTaskTurnBo updateTaskTurnBo) {
        // 1、查询数据库备份任务
        BrSysTaskInfo brSysTaskInfo = brSysTaskInfoService.getById(updateTaskTurnBo.getTaskId());
        Optional.ofNullable(brSysTaskInfo).orElseThrow(() -> new ServiceException("定时任务不存在"));
        BrSysTaskInfo update = new BrSysTaskInfo();
        update.setTaskId(brSysTaskInfo.getTaskId());
        update.setBeTurn(updateTaskTurnBo.getStatus());
        // 2、更新数据库备份任务状态
        brSysTaskInfoService.updateById(update);
    }
}
